import _regeneratorRuntime from "@babel/runtime/regenerator";
import _Stores from "choerodon-ui/pro/lib/stores";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _ColorPicker from "choerodon-ui/pro/lib/color-picker";
import _UrlField from "choerodon-ui/pro/lib/url-field";
import _EmailField from "choerodon-ui/pro/lib/email-field";
import _TextField from "choerodon-ui/pro/lib/text-field";
import _YearPicker from "choerodon-ui/pro/lib/year-picker";
import _MonthPicker from "choerodon-ui/pro/lib/month-picker";
import _WeekPicker from "choerodon-ui/pro/lib/week-picker";
import _TimePicker from "choerodon-ui/pro/lib/time-picker";
import _DateTimePicker from "choerodon-ui/pro/lib/date-time-picker";
import _DatePicker from "choerodon-ui/pro/lib/date-picker";
import _Currency from "choerodon-ui/pro/lib/currency";
import _NumberField from "choerodon-ui/pro/lib/number-field";
import _CheckBox from "choerodon-ui/pro/lib/check-box";
import _Lov from "choerodon-ui/pro/lib/lov";
import _Select from "choerodon-ui/pro/lib/select";
import _omit from "lodash/omit";
import _map from "lodash/map";
import _isObject from "lodash/isObject";
import _isString from "lodash/isString";
import React, { cloneElement, isValidElement } from 'react';
import warning from 'choerodon-ui/lib/_util/warning';
import { OPERATOR } from "./constant";
export function getEditorByField(field) {
  var lookupCode = field.get('lookupCode');
  var lookupUrl = field.get('lookupUrl');
  var lovCode = field.get('lovCode');
  var defaultValue = field.get('defaultValue');
  var type = field.type,
      name = field.name;
  var placeholder = '';

  if (defaultValue && OPERATOR[defaultValue]) {
    placeholder = defaultValue && OPERATOR[defaultValue].meaning;
    field.set('defaultValue', undefined);
  }

  if (lookupCode || _isString(lookupUrl) || type !== 'object' && (lovCode || field.options)) {
    return /*#__PURE__*/React.createElement(_Select, {
      placeholder: placeholder
    });
  }

  if (lovCode) {
    return /*#__PURE__*/React.createElement(_Lov, {
      placeholder: placeholder
    });
  }

  switch (type) {
    case 'boolean':
      return /*#__PURE__*/React.createElement(_CheckBox, {
        placeholder: placeholder
      });

    case 'number':
      return /*#__PURE__*/React.createElement(_NumberField, {
        placeholder: placeholder
      });

    case 'currency':
      return /*#__PURE__*/React.createElement(_Currency, {
        placeholder: placeholder
      });

    case 'date':
      return /*#__PURE__*/React.createElement(_DatePicker, {
        format: "YYYY-MM-DD HH:mm:ss",
        placeholder: placeholder
      });

    case 'dateTime':
      return /*#__PURE__*/React.createElement(_DateTimePicker, {
        placeholder: placeholder
      });

    case 'time':
      return /*#__PURE__*/React.createElement(_TimePicker, {
        placeholder: placeholder
      });

    case 'week':
      return /*#__PURE__*/React.createElement(_WeekPicker, {
        placeholder: placeholder
      });

    case 'month':
      return /*#__PURE__*/React.createElement(_MonthPicker, {
        placeholder: placeholder
      });

    case 'year':
      return /*#__PURE__*/React.createElement(_YearPicker, {
        placeholder: placeholder
      });

    case 'intl':
      // return <IntlField placeholder={placeholder} />;
      return /*#__PURE__*/React.createElement(_TextField, {
        placeholder: placeholder
      });

    case 'email':
      return /*#__PURE__*/React.createElement(_EmailField, {
        placeholder: placeholder
      });

    case 'url':
      return /*#__PURE__*/React.createElement(_UrlField, {
        placeholder: placeholder
      });

    case 'color':
      return /*#__PURE__*/React.createElement(_ColorPicker, {
        placeholder: placeholder
      });

    case 'string':
      return /*#__PURE__*/React.createElement(_TextField, {
        placeholder: placeholder
      });

    default:
      warning(false, "Table auto editor: No editor exists on the field<".concat(name, ">'s type<").concat(type, ">, so use the TextField as default editor"));
      return /*#__PURE__*/React.createElement(_TextField, {
        placeholder: placeholder
      });
  }
}
export function getQueryFields(dataSet) {
  var queryFields = dataSet.props.queryFields;
  var queryDataSet = dataSet.queryDataSet || dataSet.props.queryDataSet;
  var result = [];

  if (queryDataSet) {
    var fields = queryDataSet.fields || queryDataSet.props.fields;
    return _toConsumableArray(fields.entries()).reduce(function (list, _ref) {
      var _ref2 = _slicedToArray(_ref, 2),
          name = _ref2[0],
          field = _ref2[1];

      if (!field.get('bind')) {
        var props = {
          key: name,
          name: name,
          dataSet: queryDataSet
        };
        var element = queryFields[name];
        list.push(isValidElement(element) ? cloneElement(element, props) : cloneElement(getEditorByField(field), _objectSpread(_objectSpread({}, props), _isObject(element) ? element : {})));
      }

      return list;
    }, result);
  }

  return result;
}
export function setCondition(_x, _x2, _x3) {
  return _setCondition.apply(this, arguments);
}

function _setCondition() {
  _setCondition = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(dataSet, list, reset) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _map(list, /*#__PURE__*/function () {
              var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(param, index) {
                var field, lovCode, valueField, paramName, config, params;
                return _regeneratorRuntime.wrap(function _callee2$(_context2) {
                  while (1) {
                    switch (_context2.prev = _context2.next) {
                      case 0:
                        field = dataSet.getField(param.fieldName);
                        lovCode = field.get('lovCode');
                        valueField = field.get('valueField');
                        paramName = param.fieldName;

                        if (!lovCode) {
                          _context2.next = 13;
                          break;
                        }

                        if (valueField) {
                          _context2.next = 12;
                          break;
                        }

                        _context2.next = 8;
                        return _Stores.LovCodeStore.fetchConfig(lovCode);

                      case 8:
                        config = _context2.sent;
                        paramName = config.valueField;
                        _context2.next = 13;
                        break;

                      case 12:
                        paramName = valueField;

                      case 13:
                        params = param.comparator.includes('NULL') ? "".concat(paramName, ",").concat(param.comparator) : "".concat(paramName, ",").concat(param.comparator, ",").concat(param.value);
                        dataSet.setQueryParameter("search.condition.".concat(index), reset ? '' : params);
                        dataSet.setQueryParameter(paramName, '');

                      case 16:
                      case "end":
                        return _context2.stop();
                    }
                  }
                }, _callee2);
              }));

              return function (_x6, _x7) {
                return _ref4.apply(this, arguments);
              };
            }());

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _setCondition.apply(this, arguments);
}

export function setOrder(dataSet, list, reset) {
  _map(list, /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(param, index) {
      var field, lovCode, paramName, valueField, config;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              field = dataSet.getField(param.fieldName);
              lovCode = field.get('lovCode');
              paramName = param.fieldName;
              valueField = field.get('valueField');

              if (!lovCode) {
                _context.next = 13;
                break;
              }

              if (valueField) {
                _context.next = 12;
                break;
              }

              _context.next = 8;
              return _Stores.LovCodeStore.fetchConfig(lovCode);

            case 8:
              config = _context.sent;
              paramName = config.valueField;
              _context.next = 13;
              break;

            case 12:
              paramName = valueField;

            case 13:
              dataSet.setQueryParameter("search.order.".concat(index), reset ? '' : "".concat(paramName, ",").concat(param.direction));
              dataSet.setQueryParameter(paramName, '');

            case 15:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function (_x4, _x5) {
      return _ref3.apply(this, arguments);
    };
  }());
}
export function omitData(data) {
  return _omit(data, 'creationDate', 'createdBy', 'lastUpdateDate', 'lastUpdatedBy', 'objectVersionNumber', '_token', 'searchId', 'searchConditionId', 'searchQueryId', 'searchOrderId');
}